﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;
using System.Collections;
using System.Drawing;
using System.IO;

namespace MAT.Controllers
{
    class ctrAssetProfile
    {
        private frmAssetProfile _frmAssetProfile = null;

        public frmAssetProfile FrmAssetProfile
        {
            get { return _frmAssetProfile; }
            set { _frmAssetProfile = value; }
        }
        MAT_Context context = MATContext.getContext();
        static BindingSource bind = new BindingSource();
        static List<CATEGORY> categories = new List<CATEGORY>();
        static List<ASSET> assets = new List<ASSET>();
        ASSET asset = new ASSET();
        static bool status = true;
        public void frmAssetProfile_Load()
        {
            load_data();
            status = false;
            _frmAssetProfile.txtAPAssetID.DataBindings.Add("text", bind, "asset_id");
            _frmAssetProfile.txtAPAssetName.DataBindings.Add("text", bind, "ASSET_NAME");
            _frmAssetProfile.txtAPModel.DataBindings.Add("text", bind, "model_num");
            _frmAssetProfile.txtAPBarcode.DataBindings.Add("text", bind, "barcode");
            _frmAssetProfile.txtAPAcquiredDate.DataBindings.Add("text", bind, "acquired_date");
            _frmAssetProfile.txtAPExpiredDate.DataBindings.Add("Text", bind, "EXPIRED_DATE");
            _frmAssetProfile.txtAPBrand.DataBindings.Add("text", bind, "brand");
            _frmAssetProfile.txtAPPrice.DataBindings.Add("Text", bind, "PRICE");
            //_frmAssetProfile.btnAPDetail.Visible = false;
            switch (MAT.Controllers.ctrLogin.priority)
            {
                case 0: priority_admin0(); break;
                case 1: priority_admin1(); break;
                case 2: priority_admin2(); break;
                case 3: priority_user(); break;
            }
        }

        private void load_data()
        {
            bind.Clear();
            assets.Clear();
            //List all assets in database (shown for admin_0 and admin_1)
            if (MAT.Controllers.ctrLogin.priority == 0 || MAT.Controllers.ctrLogin.priority == 1)
            {
                assets = context.ASSETs.ToList();
            }

            //List all assets in the managed room (shown for admin_2)
            else
                if (MAT.Controllers.ctrLogin.priority == 2)
                {
                    //List all assets of members in the managed room
                    List<OWNER> owners = context.OWNERS.Where(o => o.MANAGE == MAT.Controllers.ctrLogin.employee_ID).ToList();
                    foreach (OWNER own in owners)
                    {
                        List<ASSET> tem = context.ASSETs.Where(ass => ass.EMPLOYEE_ID == own.EMPLOYEE_ID).ToList();
                        foreach (ASSET a in tem)
                        {
                            assets.Add(a);
                        }
                    }
                    //List assets of admin_2
                    foreach (ASSET a in context.ASSETs.Where(am => am.EMPLOYEE_ID == MAT.Controllers.ctrLogin.employee_ID).ToList())
                    {
                        assets.Add(a);
                    }
                }
                else
                    if (MAT.Controllers.ctrLogin.priority == 3)
                    {
                        foreach (ASSET a in context.ASSETs.Where(ass => ass.EMPLOYEE_ID == MAT.Controllers.ctrLogin.employee_ID).ToList())
                        {
                            assets.Add(a);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Can not load data!");
                        return;
                    }
            _frmAssetProfile.textBox1.Text = assets.Count.ToString();

            //List<REQUEST_MAINTENANCE> maintenances = context.REQUEST_MAINTENANCE.Where(mai => mai.REQ_STATUS == "RE004").ToList();
            //if (maintenances != null)
            //{
            //    for (int i = 0; i < maintenances.Count; i++)
            //    {
            //        for (int j = 0; j < assets.Count; j++)
            //        {
            //            if (maintenances[i].ASSET == assets[j])
            //            {
            //                _frmAssetProfile.grvData.Rows[j].Cells[12].Value = "Maintenance";
            //                break;
            //            }
            //        }
            //    }
            //}
            //List<REQUEST_LIQUIDATION> liquidations = context.REQUEST_LIQUIDATION.Where(liq => liq.REQ_STATUS == "RE004").ToList();
            //if (liquidations != null)
            //{
            //    for (int i = 0; i < liquidations.Count; i++)
            //    {
            //        for (int j = 0; j < assets.Count; j++)
            //        {
            //            if (liquidations[i].ASSET.ASSET_ID == assets[j].ASSET_ID)
            //            {
            //                _frmAssetProfile.grvData.Rows[j].Cells[12].Value = "Liquidation";
            //                break;
            //            }
            //        }
            //    }
            //}

            //Load catagories into combobox
            foreach (ASSET a in assets)
            {
                if (!categories.Contains(a.CATEGORY))
                    categories.Add(a.CATEGORY);
            }
            _frmAssetProfile.cmbAPCategory.DataSource = categories.ToList();
            _frmAssetProfile.cmbAPCategory.DisplayMember = "CATEGORY_NAME";
            _frmAssetProfile.cmbAPCategory.ValueMember = "CATEGORY_ID";

            bind.DataSource = assets.ToList();
            _frmAssetProfile.grvData.DataSource = bind;

            //Load data for STATUS column
            load_column_status();
        }

        public void cmbAPCategory_SelectedIndexChanged()
        {
            if (!status)
            {
                _frmAssetProfile.checkBoxAll.Checked = false;
                _frmAssetProfile.checkBoxAll.Enabled = true;
                bind.Clear();
                string categoryID = _frmAssetProfile.cmbAPCategory.SelectedValue.ToString();
                List <ASSET> assets_temp = assets.Where(ass => ass.CATEGORY_ID == categoryID).ToList();
                bind.DataSource = assets_temp;
            }
        }

        public void txtAPAssetID_TextChanged()
        {
            
            string assetID = _frmAssetProfile.txtAPAssetID.Text;
            asset = context.ASSETs.FirstOrDefault(ass => ass.ASSET_ID == assetID);
            //Show Onwer
            if (asset == null)
            {
                _frmAssetProfile.txtAPOwner.Text = "";
                _frmAssetProfile.ptbAPImage.Image = null;
                return;
            }
            if (asset.EMPLOYEE_ID == null) return;
            _frmAssetProfile.txtAPOwner.Text = context.OWNERS.FirstOrDefault(o => o.EMPLOYEE_ID.Trim() == asset.EMPLOYEE_ID.Trim()).EMPLOYEE_NAME;
            //Show image
            MemoryStream stream = new MemoryStream(asset.IMAGE.ToArray());
            Image img = Image.FromStream(stream);
            if (img != null)
            {
                _frmAssetProfile.ptbAPImage.Image = img;
            }
            _frmAssetProfile.txtAPCategory.Text = context.CATEGORies.FirstOrDefault(ca => ca.CATEGORY_ID == asset.CATEGORY_ID).CATEGORY_NAME;
        }

        public void checkBoxAll_CheckedChanged()
        {
            if (_frmAssetProfile.checkBoxAll.Checked)
            {
                _frmAssetProfile.checkBoxAll.Enabled = false;
                status = true;
                load_data();
                status = false;
            }
        }

        private void load_column_status()
        {
            ////List all assets were approved in REQUEST_MAINTENANCE table
            //List<REQUEST_MAINTENANCE> maintenances = context.REQUEST_MAINTENANCE.Where(mai => mai.REQ_STATUS == "RE004").ToList();
            //if (maintenances != null)
            //{
            //    foreach (REQUEST_MAINTENANCE rM in maintenances)
            //        assets.Add(rM.ASSET);
            //}
            //if (assets != null)
            //{
            //    for (int i = 0; i < assets.Count; i++)
            //        for (int j = 0; j < _frmAssetProfile.grvData.RowCount; j++)
            //        {
            //            if (assets[i].ASSET_ID == _frmAssetProfile.grvData.Rows[j].Cells[0].Value)
            //            {
            //                _frmAssetProfile.grvData.Rows[j].Cells[12].Value = "Maintenance";
            //                break;
            //            }
            //        }
            //}

            //assets.Clear();
            ////List all assets were approved in REQUEST_LIQUIDATION table
            //List<REQUEST_LIQUIDATION> liquidations = context.REQUEST_LIQUIDATION.Where(liq => liq.REQ_STATUS == "RE004").ToList();
            //if (liquidations != null)
            //{
            //    foreach (REQUEST_LIQUIDATION rL in liquidations)
            //        assets.Add(rL.ASSET);
            //}
            //if (assets != null)
            //{
            //    for (int i = 0; i < assets.Count; i++)
            //        for (int j = 0; j < _frmAssetProfile.grvData.RowCount; j++)
            //        {
            //            if (assets[i].ASSET_ID == (string)_frmAssetProfile.grvData.Rows[j].Cells[0].Value)
            //            {
            //                MessageBox.Show((string)_frmAssetProfile.grvData.Rows[15].Cells[0].Value);
            //                _frmAssetProfile.grvData.Rows[j].Cells[12].Value = "Liquidation";
            //                break;
            //            }
            //        }
            //}
            List<REQUEST_MAINTENANCE> maintenances = context.REQUEST_MAINTENANCE.Where(mai => mai.REQ_STATUS == "RE004").ToList();
            if (maintenances != null)
            {
                for (int i = 0; i < maintenances.Count; i++)
                {
                    for (int j = 0; j < assets.Count; j++)
                    {
                        if (maintenances[i].ASSET == assets[j])
                        {
                            _frmAssetProfile.grvData.Rows[j].Cells[12].Value = "Maintenance";
                            break;
                        }
                    }
                }
            }
            List<REQUEST_LIQUIDATION> liquidations = context.REQUEST_LIQUIDATION.Where(liq => liq.REQ_STATUS == "RE004").ToList();
            if (liquidations != null)
            {
                for (int i = 0; i < liquidations.Count; i++)
                {
                    for (int j = 0; j < assets.Count; j++)
                    {
                        if (liquidations[i].ASSET.ASSET_ID == assets[j].ASSET_ID)
                        {
                            MessageBox.Show("" + j);
                            _frmAssetProfile.grvData.Rows[j].Cells[12].Value = "Liquidation";
                            break;
                        }
                    }
                }
            }
        }

        public void deleteToolStripMenuItem_Click()
        {
            try
            {
                ASSET asset = new ASSET();
                asset = context.ASSETs.FirstOrDefault(a => a.ASSET_ID.Trim() == _frmAssetProfile.txtAPAssetID.Text.Trim());
                if (asset != null)
                {
                    context.ASSETs.DeleteObject(asset);
                    context.SaveChanges();
                    MessageBox.Show("Successful");
                    load_data();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("You could not delete this item!");
            }
        }
        
        public void moveToolStripMenuItem_Click()
        {
            frmMoveAsset moveAsset = new frmMoveAsset();
            moveAsset.ShowDialog();
            load_data();
        }

        public void reassignToolStripMenuItem_Click()
        {
            frmAssignment assign = new frmAssignment();
            assign.txtAsset.Text = _frmAssetProfile.txtAPAssetID.Text.Trim();
            assign.ShowDialog();
            load_data();
        }
        
        private void priority_admin0()
        {
            _frmAssetProfile.moveToolStripMenuItem.Enabled = true;
            _frmAssetProfile.deleteToolStripMenuItem.Enabled = true;
            _frmAssetProfile.reassignToolStripMenuItem.Enabled = true;
        }
        private void priority_admin1()
        {
            _frmAssetProfile.moveToolStripMenuItem.Enabled = true;
            _frmAssetProfile.deleteToolStripMenuItem.Enabled = true;
            _frmAssetProfile.reassignToolStripMenuItem.Enabled = true;
        }
        private void priority_admin2()
        {
            _frmAssetProfile.moveToolStripMenuItem.Enabled = true;
            _frmAssetProfile.deleteToolStripMenuItem.Enabled = true;
            _frmAssetProfile.reassignToolStripMenuItem.Enabled = true;
        }
        private void priority_user()
        {
            _frmAssetProfile.moveToolStripMenuItem.Enabled = false; 
            _frmAssetProfile.deleteToolStripMenuItem.Enabled = false;
            _frmAssetProfile.reassignToolStripMenuItem.Enabled =false;
        }
    }
}